package com.glela.common.aspect;

import java.util.HashMap;
import java.util.Map;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.response.ResponseBaseModel;

/**
 * api Controller层切面
 * 
 * @author liujie
 */
@Aspect
@Component
@Order(1)
public class IpAccessControllerAspect
{
    public static Map<String, Long> illegalIpMap = new HashMap<String, Long>();

	static protected final Logger logger = LoggerFactory.getLogger(IpAccessControllerAspect.class);
  
 	@Around("@annotation(com.glela.common.annotation.IpAccessAnnotation)")
	public Object around(ProceedingJoinPoint pjp)
	{
// 		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder  
//                .getRequestAttributes()).getRequest(); 
 		/*String fromIp = request.getRemoteAddr();
 		String limitIp = Config.getString("ipLimte");*/
 		
  		return handleCause(pjp);
	}

	private Object handleCause(ProceedingJoinPoint pjp)
	{
		try {
 			return pjp.proceed();
		}
		catch (Throwable e) {
			 logger.error(e.getMessage());
//			 if(e instanceof BusinessException){
//				 return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
// 			 }
			 return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
 		}
 	}
 }
